package com.tusuo.stds.ui.questionbank;

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.QuestionBankGroup;
import com.tusuo.stds.util.Utils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

import cn.carbs.android.avatarimageview.library.AvatarImageView;

/**
 * @author J.Huang
 * @version 1.0
 */
public class QuestionBankGroupListAdapter extends BaseAdapter {


    private static int sSelectedId = 0;
    private static boolean sFirst = true;
    private int selectedIndex;

    private List<QuestionBankGroup.QuestionGroup> dataList;
    private LayoutInflater layoutInflater;
    private Activity mActivity;

    public QuestionBankGroupListAdapter(Activity activity, List<QuestionBankGroup.QuestionGroup> dataList, int selectedIndex) {
        this.mActivity = activity;
        this.dataList = dataList;
        this.layoutInflater = LayoutInflater.from(activity);
        sFirst = true;
        sSelectedId = selectedIndex;
    }

    private final class Component {
        TextView tvTitle;
        TagFlowLayout flowLayout;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public QuestionBankGroup.QuestionGroup getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Component component = null;
        if (convertView == null) {
            component = new Component();
            // 获得组件，实例化组件
            convertView = layoutInflater.inflate(R.layout.question_bank_group_list_item, null);

            component.tvTitle = (TextView) convertView.findViewById(R.id.question_bank_group_list_item_tv_title);
            component.flowLayout = (TagFlowLayout) convertView.findViewById(R.id.question_bank_group_list_item_flowlayout);

            convertView.setTag(component);
        } else {
            component = (Component) convertView.getTag();
        }

        QuestionBankGroup.QuestionGroup questionGroup = dataList.get(position);

        // 绑定数据
        component.tvTitle.setText(questionGroup.Name);

        TagAdapter<Integer> tagAdapter = new TagAdapter<Integer>(questionGroup.List) {
            @Override
            public View getView(FlowLayout parent, int position, Integer i) {
                AvatarIv aiv = (AvatarIv) layoutInflater.inflate(R.layout.question_bank_group_list_iv, parent, false);

                if (i == sSelectedId) {

                    aiv.setBackgroundResource(R.drawable.question_bank_group_item_selected);
                    aiv.setTextAndColor(String.valueOf(position + 1), ContextCompat.getColor(mActivity, R.color.colorThemeSecondary));
                    aiv.setBoarderColor(ContextCompat.getColor(mActivity, R.color.colorThemeSecondary));
                } else {

                    aiv.setBackgroundColor(ContextCompat.getColor(mActivity, R.color.transparent));
                    aiv.setTextAndColor(String.valueOf(position + 1), ContextCompat.getColor(mActivity, R.color.general_text_color));
                    aiv.setBoarderColor(ContextCompat.getColor(mActivity, R.color.general_text_color));

                }

                aiv.setTag(i);

                return aiv;
            }
        };


        component.flowLayout.setAdapter(tagAdapter);

        component.flowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                sSelectedId = (int) view.getTag();

                if (mActivity instanceof QuestionBankTempActivity_) {
                    ((QuestionBankTempActivity_) mActivity).showCertainQuestion(sSelectedId);
                }
                return true;
            }
        });

        return convertView;
    }
}
